﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using appDataContracts;
using appClient;
using appInterfaces;
using System.ServiceModel;

namespace appClient
{
	public partial class Order : System.Web.UI.Page
	{

		IClient client;
		int key;

		protected void Page_Load(object sender, EventArgs e)
		{
			client = new ChannelFactory<IClient>("clientHttp").CreateChannel();

			//key = client.addOrder(Session["user"], new SerializedOrder(Session["caddie"]
			if (Session["user"] != null)
			{
				if (Session["caddie"] != null && ((List<CaddieProduct>)Session["caddie"]).Count != 0)
				{
					//SerializedOrder a = new SerializedOrder(
					
				}
				else
					Response.Redirect("Default.aspx");
			}
			else
				Response.Redirect("Default.aspx");
		}

		protected void OrderButton_Click(object sender, EventArgs e)
		{
			DateTime cExp = cardExp.SelectedDate;
			string cNum = cardNum.Text;
			string cPic = cardPic.Text;
			string cType = cardType.SelectedValue;

			decimal totalToPay=0;
			List<SerializedContain> cart = new List<SerializedContain>();

			foreach(CaddieProduct cp in ((List<CaddieProduct>) Session["caddie"]))
			{
				totalToPay += cp.Quantity * client.getProductInfo(cp.Id).PRODUCTPRICE;
				SerializedContain article = new SerializedContain(cp.Id, 0, false, cp.Quantity);
				cart.Add(article);
			}




			SerializedOrder order = new SerializedOrder(0, 
				((SerializedClient)Session["user"]).CLIENTID, 
				DateTime.Now, 
				true, 
				totalToPay, 
				((SerializedClient)Session["user"]).CLIENTFIRSTNAME + " " + ((SerializedClient)Session["user"]).CLIENTLASTNAME, 
				cNum, 
				cExp, 
				cPic, 
				cType, 
				cart);

			key = client.addOrder((SerializedClient)Session["user"], order);
			Response.Redirect("ValidCommand.aspx?key="+key);
		}
	}
}